// Copyright 2017 Baidu Inc. All Rights Reserved.
// Author: Su Hao (suhao@baidu.com)
//
// Description: Report the Play Events.

#include <mbed.h>
#include "duer_play_command.h"
#include "duer_log.h"
#include "baidu_ca_scheduler.h"

using duer::PlayCommand;

PlayCommand::PlayCommand()
    : _data(NULL)
{
}

PlayCommand::~PlayCommand()
{
    if (_data != NULL) {
        baidu_json_Delete(_data);
        _data = NULL;
    }
}

int PlayCommand::set_action(baidu_json *action)

{
    int rs = OK;

    DUER_LOGI("PlayCommand::set_action begin");

    if (_data) {
        baidu_json_Delete(_data);
        _data = NULL;
    }

    do {
        baidu_json *temp = NULL;
        baidu_json *payload = NULL;

        if (action == NULL) {
            DUER_LOGE("Parse action Failed");
            rs = ERR_MEMORY_OVERFLOW;
            break;
        }

        temp = baidu_json_GetObjectItem(action, "speech");
        if (temp) {
            temp = baidu_json_GetObjectItem(temp, "url");
            _has_speech = (temp != NULL && temp->valuestring != NULL && temp->valuestring[0] != '\0') ? true : false;
        } else {
            _has_speech = false;
        }

        _data = baidu_json_CreateObject();
        if (_data == NULL) {
            DUER_LOGE("Create Object Failed");
            rs = ERR_MEMORY_OVERFLOW;
            break;
        }

        payload = baidu_json_GetObjectItem(action, "payload");
        if (payload == NULL) {
            DUER_LOGW("No payload found!");
            rs = ERR_UNKOWN_ACTION;
            break;
        }

        temp = baidu_json_GetObjectItem(payload, "audio_item_id");
        if (temp == NULL || temp->valuestring == NULL) {
            DUER_LOGI("No audio_item_id found!");
            rs = ERR_UNKOWN_ACTION;
            break;
        }

        baidu_json_AddStringToObject(_data, "token", temp->valuestring);

        temp = baidu_json_GetObjectItem(payload, "message_id");
        if (temp == NULL || temp->valuestring == NULL) {
            DUER_LOGW("No message_id found!");
            rs = ERR_UNKOWN_ACTION;
            break;
        }

        baidu_json_AddStringToObject(_data, "message_id", temp->valuestring);

        temp = baidu_json_CreateObject();
        if (temp == NULL) {
            DUER_LOGE("Create Object Failed");
            rs = ERR_MEMORY_OVERFLOW;
            break;
        }

        baidu_json_AddItemToObject(temp, "data", _data);
        _data = temp;

        temp = baidu_json_GetObjectItem(action, "source_type");
        if (temp == NULL || temp->valuestring == NULL) {
            DUER_LOGI("No source_type found!");
        } else {
            baidu_json_AddStringToObject(_data, "source_type", temp->valuestring);
        }


        baidu_json_AddNullToObject(_data, "type");
    } while (false);

    if (rs != OK && _data != NULL) {
        baidu_json_Delete(_data);
        _data = NULL;
    }

    DUER_LOGI("PlayCommand::set_action end rs = %d", rs);

    return rs;
}

int PlayCommand::set_location(PlayCommand::Location location)
{
    return 0;
}

int PlayCommand::start()
{
    if (!_has_speech) {
        return report("play_start");
    }
    return 0;
}

int PlayCommand::stop()
{
    if (!_has_speech) {
        return report("play_stop");
    } else {
        _has_speech = false;
    }
    return 0;
}

int PlayCommand::end()
{
    int rs = 0;
    if (!_has_speech) {
        rs = report("play_end");
        if (_data != NULL) {
            baidu_json_Delete(_data);
            _data = NULL;
        }
    } else {
        _has_speech = false;
    }
    return rs;
}

int PlayCommand::next()
{
    if (!_has_speech) {
        return report("play_next");
    }
    return 0;
}

int PlayCommand::previous()
{
    if (!_has_speech) {
        return report("play_pre");
    }
    return 0;
}

int PlayCommand::repeat()
{
    if (!_has_speech) {
        return report("play_repeat");
    }
    return 0;
}

bool PlayCommand::has_data() const
{
    return _has_speech || _data != NULL ? true : false;
}

int PlayCommand::report(const char *action)
{
    int rs = -1;
    if (_data) {
        baidu_json *value = baidu_json_CreateObject();
        baidu_json *temp = NULL;
        if (value) {
            baidu_json_DeleteItemFromObject(_data, "id");
            Scheduler::instance().update_topic_id();
            baidu_json_AddNumberToObject(_data, "id", Scheduler::instance().get_topic_id());
            baidu_json_AddStringToObject(value, "status", action);
            baidu_json_AddItemReferenceToObject(value, "payload", _data);

            temp = baidu_json_CreateObject();
            if (temp) {
                baidu_json_AddItemToObject(temp, "play_status", value);
                value = temp;
                rs = duer::Scheduler::instance().report(value);
            }

            baidu_json_Delete(value);
        }
    }
    return rs;
}
